package com.example.demo.service;

import com.example.demo.entity.two.DeviceItem;
import com.example.demo.entity.two.DevicePage;
import com.example.demo.entity.two.MyDevicePage;
import com.example.demo.entity.two.UnitInfo;
import com.example.demo.repository.two.*;
import com.example.demo.vo.GeneratorParamVO;
import com.example.demo.vo.TurbineParamVO;
import com.example.demo.vo.UnitParamVO;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;

import java.util.List;

/**
 * 静态数据服务，主要为台账展示
 * Created by 落羽 on 2017/9/6
 */
@Service
public class StaticDataService {
    Logger logger = LoggerFactory.getLogger(StaticDataService.class);

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private UnitInfoRepository unitInfoRepository;

    @Autowired
    private MyDevicePageRepository myDevicePageRepository;

    @Autowired
    private DevicePageRepository devicePageRepository;

    @Autowired
    private DeviceItemRepository deviceItemRepository;

    /**
     *
     * @param Id myDevicePage的Unit_Id
     * @return 机组的参数信息
     */
    public UnitParamVO getUnitParamVO(int Id) {
        UnitParamVO unitParamVO = new UnitParamVO();
        // 这里数据库里的都是一个水轮机一个发电机且水轮机在前
        List<MyDevicePage> myDevicePageList = myDevicePageRepository.findByUnitIDOrderByMyDevicePageID(Id);
        if (myDevicePageList != null && myDevicePageList.size() > 0){
            MyDevicePage myDevicePage = myDevicePageList.get(0);
            MyDevicePage myDevicePage1 = myDevicePageList.get(1);
            // 因为没用redis缓存 暂时这样处理
//            DevicePage devicePage = myDevicePage.getDevicePage();
//            DevicePage devicePage1 = myDevicePage1.getDevicePage();
            DevicePage devicePage = devicePageRepository.findByDevicePageId(myDevicePage.getDevicePage());
            DevicePage devicePage1 = devicePageRepository.findByDevicePageId(myDevicePage1.getDevicePage());
            List<DeviceItem> deviceItemList = deviceItemRepository.findDeviceItemList(devicePage.getDevicePageId());
            List<DeviceItem> deviceItemList1 = deviceItemRepository.findDeviceItemList(devicePage1.getDevicePageId());
            // 获得水轮机型式 为第6个属性
            unitParamVO.setHydraulicTurbineType(deviceItemList.get(5).getInitValue());
            // 发电机型式 暂无
            unitParamVO.setGeneratorModel("全伞式");
            //
            unitParamVO.setRatedCapacity(deviceItemList1.get(6).getInitValue());
            unitParamVO.setRatedVoltage(deviceItemList1.get(4).getInitValue());
            unitParamVO.setRatedSpeed(deviceItemList.get(2).getInitValue());
            unitParamVO.setDesign(" ");
            unitParamVO.setDate(" ");
        }else {
            System.out.println("不存在此机组");
            return null;
        }

        return unitParamVO;
    }

    /**
     *
     * @param Id myDevicePage的Unit_Id
     * @return 发电机的参数信息
     */
    public GeneratorParamVO getGeneratorParamVO(int Id) {
        GeneratorParamVO generatorParamVO = new GeneratorParamVO();

        List<MyDevicePage> myDevicePageList = myDevicePageRepository.findByUnitIDOrderByMyDevicePageID(Id);
        if (null != myDevicePageList && myDevicePageList.size() > 0){
            MyDevicePage myDevicePage = myDevicePageList.get(1);
//            DevicePage devicePage = myDevicePage.getDevicePage();
            DevicePage devicePage = devicePageRepository.findByDevicePageId(myDevicePage.getDevicePage());
            List<DeviceItem> deviceItemList = deviceItemRepository.findDeviceItemList(myDevicePage.getDevicePage());

            generatorParamVO.setGeneratorModel(devicePage.getDevicePageName());
            generatorParamVO.setGeneratorManufacturer(devicePage.getDevicePageName());
            generatorParamVO.setGeneratorType(" ");
            generatorParamVO.setRatedCapacity(deviceItemList.get(6).getInitValue());
            generatorParamVO.setRatedVoltage(deviceItemList.get(4).getInitValue());
            generatorParamVO.setRatedCurrent(deviceItemList.get(5).getInitValue());
        }else {
            System.out.println("不存在此电机");
            return null;
        }

        return generatorParamVO;
    }

    /**
     *
     * @param Id myDevicePage的Unit_Id
     * @return 水轮机的参数信息
     */
    public TurbineParamVO getTurbineParamVO(int Id) {
        TurbineParamVO turbineParamVO = new TurbineParamVO();

        List<MyDevicePage> myDevicePageList = myDevicePageRepository.findByUnitIDOrderByMyDevicePageID(Id);
        if (null != myDevicePageList && myDevicePageList.size() > 0){
            MyDevicePage myDevicePage = myDevicePageList.get(0);
//            DevicePage devicePage = myDevicePage.getDevicePage();
            DevicePage devicePage = devicePageRepository.findByDevicePageId(myDevicePage.getDevicePage());
            List<DeviceItem> deviceItemList = deviceItemRepository.findDeviceItemList(myDevicePage.getDevicePage());

            turbineParamVO.setHydraulicTurbineType(devicePage.getDevicePageName());
            turbineParamVO.setHydraulicTurbineManufacturer(devicePage.getDevicePageName());
            turbineParamVO.setSpindleLayout(deviceItemList.get(5).getInitValue());
            turbineParamVO.setDesign(" ");
            turbineParamVO.setDiameter(" ");
            turbineParamVO.setNumber(deviceItemList.get(0).getInitValue());
        }else {
            System.out.println("不存在此水轮机");
            return null;
        }

        return turbineParamVO;
    }
}
